package user_v1

import (
	"context"
	"gf-xb-api/internal/model/dto"

	"github.com/gogf/gf/v2/frame/g"
)

func (s *sUser) CanAccessApi(ctx context.Context, in *dto.UserCanAccessApi) (output bool, err error) {

	c, err := g.DB().Model("sys_user_role ur").
		InnerJoin("sys_role rl", "ur.role_id=rl.Id or ur.role_id=rl.parent_id").
		InnerJoin("sys_role_api ra", "rl.id=ra.role_id").
		InnerJoin("sys_api sa", "ra.api_id=sa.id").
		Where("ur.user_id = ? and ur.Status = 1 and ra.Status = 1 and sa.route = ? and sa.method = ? and sa.Status = 1", in.UserId, in.ApiRoute, in.ApiMethod).
		Count(ctx)

	if err != nil {
		return false, err
	}

	if c == 0 {
		return false, nil
	}

	return true, nil
}
